package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.bean.LoginBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.LoginModel;

public class LoginActivity extends BaseActivity {
    EditText username;
    EditText password;
    Button btn_dl;
    TextView btn_zc;
    private SharedPreferences ap;
    private String userid;
    CheckBox jzmm;
    ImageView iv_see_password;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_login2);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        btn_dl = (Button) findViewById(R.id.btn_dl);
        btn_zc = (TextView) findViewById(R.id.btn_zc);
        iv_see_password = (ImageView) findViewById(R.id.iv_see_password);
        jzmm = (CheckBox) findViewById(R.id.jzmm);
        boolean flagmm = getUer_remember();
        String uname = getuser();
        String psd = getpass();

        if (flagmm) {
            username.setText(uname);
            password.setText(psd);
            jzmm.setChecked(flagmm);
        }
    }

    @Override
    void initEvent() {

        btn_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LoginModel loginModel = new LoginModel();
                loginModel.getUser(username.getText().toString(), password.getText().toString(), tListener);

            }
        });

        btn_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent1);
            }
        });

        iv_see_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisibility();    //改变图片并设置输入框的文本可见或不可见

            }
        });
    }

    @Override
    void initDate() {

    }


    TListener<LoginBean> tListener = new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            String a = loginBean.getCode();
            userid = loginBean.getUid();

            if (a.equals("1")) {
                saveUserinfo();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            } else if (a.equals("2")) {

                Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            } else if (a.equals("3")) {
                Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            } else if (a.equals("4")) {
                Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
            } else if (a.equals("5")) {
                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
        }
    };



    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String info1 = data.getStringExtra("REGNAME");
                    String info2 = data.getStringExtra("REGPASS");
                    username.setText(info1);
                    password.setText(info2);
                }
        }
    }


    private void saveUserinfo() {
        String uid = userid;
        SharedPreferences.Editor editor = getSharedPreferences("userInfo", MODE_PRIVATE).edit();
        editor.putString("userid", uid);
        editor.putBoolean("remember", jzmm.isChecked());
        if (jzmm.isChecked()) {
            editor.putString("username", username.getText().toString());
            editor.putString("userpass", password.getText().toString());
        }
        editor.commit();
    }

    /**1
     * 设置密码可见和不可见的相互转换
     */
    private void setPasswordVisibility() {
        if (iv_see_password.isSelected()) {
            iv_see_password.setSelected(false);
            //密码不可见
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_password.setSelected(true);
            //密码可见
            password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

    }


}